package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.entity.ShoppingSysAdmin;
import com.zhang.mapper.ShoppingSysAdminMapper;
import com.zhang.service.ShoppingSysAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhangD
 * @since 2021-07-13
 */
@Service
public class ShoppingSysAdminServiceImpl extends ServiceImpl<ShoppingSysAdminMapper, ShoppingSysAdmin> implements ShoppingSysAdminService {

    @Autowired
    private ShoppingSysAdminMapper shoppingSysAdminMapper;

    @Override
    public void addAdmin(AdminVo adminVo,ShoppingSysAdmin shoppingSysAdmin) {
        shoppingSysAdmin.setLoginName(adminVo.getLoginName());
        shoppingSysAdmin.setAdminName(adminVo.getAdminName());
        shoppingSysAdmin.setPassword(adminVo.getPassword());
        shoppingSysAdmin.setEmail(adminVo.getEmail());
        shoppingSysAdmin.setMobile(adminVo.getMobile());
        shoppingSysAdmin.setAge(adminVo.getAge());
        shoppingSysAdmin.setSex(adminVo.getSex());
        shoppingSysAdmin.setAddress(adminVo.getAddress());
    }

    @Override
    public void updateAdmin(AdminVo adminVo) {


        ShoppingSysAdmin shoppingSysAdmin = shoppingSysAdminMapper.select(adminVo.getId());
        shoppingSysAdmin.setLoginName(adminVo.getLoginName());
        shoppingSysAdmin.setAdminName(adminVo.getAdminName());
        shoppingSysAdmin.setPassword(adminVo.getPassword());
        shoppingSysAdmin.setEmail(adminVo.getEmail());
        shoppingSysAdmin.setMobile(adminVo.getMobile());
        shoppingSysAdmin.setAge(adminVo.getAge());
        shoppingSysAdmin.setSex(adminVo.getSex());
        shoppingSysAdmin.setAddress(adminVo.getAddress());
        shoppingSysAdminMapper.updateById(shoppingSysAdmin);

    }

    @Override
    public void updatePassword(String password, String oldPassword1, String oldPassword2) {
        QueryWrapper<ShoppingSysAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq("password",password);
        ShoppingSysAdmin admin = new ShoppingSysAdmin();
        if (password!=oldPassword1&&oldPassword1.equals(oldPassword2)) {
                admin.setPassword(oldPassword1);
                shoppingSysAdminMapper.update(admin,wrapper);
        }
    }
}
